<?php
class ControllerToolCollection extends AdminController {
	public function index() {
		M('tool/collection');
		$this->getList();
	}
	
	public function insert() {
		M('tool/collection');
		if ($this->request->isPost()) {
			$this->model_tool_collection->addNode($this->request->post);
	
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('tool/collection'));
		}
		$this->getForm();
	}
	
	public function copy() {
		M('tool/collection');		
		$this->getForm(true);
	}
	
	public function update() {
		M('tool/collection');
		if ($this->request->isPost()) {
			$this->model_tool_collection->editNode($this->request->get['id'], $this->request->post);
	
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('tool/collection'));
		}
		$this->getForm();
	}
	
	public function delete() {
		M('tool/collection');
		if ($this->request->post['selected']) {
			$this->model_tool_collection->deleteNode($this->request->post['selected']);
			$this->session->set_flashdata('success', $this->_['text_success']);
			$this->redirect(UA('tool/collection'));
		}
		$this->getList();
	}
	
	protected function getList() {
		$this->document->setTitle($this->_['heading_title']);
	
		$qf = new Query_filter();
		$total_nodes = $this->model_tool_collection->getTotalNodes();
		$this->data['nodes'] = $this->model_tool_collection->getNodes($qf);
		$this->data['pagination'] = $this->pagination($total_nodes)->render();
	
		$this->display('tool/collection_node_list.tpl');
	}
	
	protected function getForm($copy = false) {
		$this->load->helper('collection');
		$this->document->addScript(ROOT . 'static/javascript/jquery/jquery.validate.js');
		$this->document->setTitle($this->_['heading_title']);
		$id = G('id');
		if ($id) {
			if ($copy) {
				$this->data['action'] = UA('tool/collection/insert');
			}
			else {
				$this->data['action'] = UA('tool/collection/update', "id=$id");
			}
			if (!$this->request->isPost()) {
				$this->vo->data = $this->model_tool_collection->getNode($id);
			}
		}
		else {
			$this->data['action'] = UA('tool/collection/insert');
		}
	
		$this->display('tool/collection_node_form.tpl');
	}
	
	public function ctest() {
		M('tool/collection');
		$this->load->helper('collection');
		
		$step = G('step', 1);
		$id = G('id');
		$node = $this->model_tool_collection->getNode($id);
		if ($step == 1) {
			$urls = Collection::url_list($node);
			$this->data['url'] = $urls[0];
			$this->data['urllist'] = Collection::get_url_lists($urls[0], $node);
		}
		else {
			$url = $this->request->get['url'];
			$this->data['url'] = $url;
			$this->data['content'] = Collection::get_content($url, $node);
		}
		$this->data['node'] = $node;
		$this->display('tool/collection_ctest.tpl');
	}
	
	public function clist() {
		$id = G('id');
		M('tool/collection');
		$this->load->helper('collection');
		$node = $this->model_tool_collection->getNode($id);
		$urls = Collection::url_list($node);
		$total = count($urls);
		$qstr = 'do=1';
		$p = G('p', 0);
		$do = G('do', 0);
		$end = 0;
		$cp = 0;
		if ($total > 0) {
			if($do) {
				$pagesize = $node['col_per_list'];
				$offset = $p * $pagesize;
				$parts = array_slice($urls, $offset, $pagesize);
				foreach($parts as $url) {
					$data = Collection::get_url_lists($url, $node);
					$this->model_tool_collection->saveUrls($id, $data);
					$this->model_tool_collection->editNode($id, array('last_time' => $GLOBALS['V5_TIME'], 'status' => 1));
				}
				$qstr .= '&p=' . ($p + 1);
				$cp = $offset + count($parts);
				$cp < $total or $end = 1;
			}
		}
		else $end = 1;
		if ($end) {
			$this->data['message'] = '采集完成';	
		}
		else {
			$this->data['message'] = "<p>节点：$node[site_name] :: $node[node_title]</p><p>已采集：$cp/$total</p><p>采集将继续，请稍侯...</p>";
			$this->data['redirect'] = UA('tool/collection/clist', "id=$id&$qstr");
			$this->data['wait'] = $node['col_sleep'];
		}
		
		$this->document->setTitle($this->_['heading_title']);
		$this->data['continue'] = UA('tool/collection');
		$this->data['heading_title'] = '产品采集';
		
		$this->display('common/message.tpl');
	}
	
	public function ccontent() {
		$id = G('id');
		M('tool/collection');
		$this->data['node'] = $this->model_tool_collection->getNode($id);
		$qf = new Query_filter(null, false);
		$qf->set('nodeid', $id);
		$qf->set('status', 0);
		$this->data['total'] = $this->model_tool_collection->getTotalContents($qf);
		$this->display('tool/collection_ccontent.tpl');
	}
	
	public function cthread() {
		set_time_limit(0);
		$id = G('id');
		$init = G('init', 0);
		M('tool/collection');
		$node = $this->model_tool_collection->getNode($id);
		if (!$init) {
			$urls = $this->model_tool_collection->getUrls($id, $node['col_per_cont']);
			if ($urls) {
				$this->load->helper('collection');
				foreach($urls as $url) {
					$data = Collection::get_content($url['url'], $node);
					$data['coltime'] = time();
					$data['status'] = 1;
					$this->model_tool_collection->editContent($url['id'], $data);
				}
				$this->model_tool_collection->editNode($id, array('last_time' => $GLOBALS['V5_TIME'], 'status' => 1));
				$this->data['urls'] = $urls;
			}
			else {
				$this->model_tool_collection->editNode($id, array('status' => 2));
			}
		}
		$this->data['init'] = $init;
		$this->data['node'] = $node;
		$this->display('tool/collection_cthread.tpl', false);
	}
	
	public function cclear() {
		M('tool/collection');
		$this->model_tool_collection->clearNode($this->request->get['id']);
		$this->session->set_flashdata('success', $this->_['text_success']);
		$this->redirect(UA('tool/collection'));
	}
	
	public function cdata() {
		$this->data['statuses'] = array(0 => '未采集', 1 => '已采集', 2 => '已入库', 9 => '采集中');
		
		$id = G('id');
		M('tool/collection');
		$node = $this->model_tool_collection->getNode($id);
		$this->data['node'] = $node;
		$this->data['token'] = $this->session->data['token'];
		
		if (isset($this->request->post['selected'])) {
			if (isset($this->request->get['delete'])) {		
				$this->model_tool_collection->deleteContent($this->request->post['selected'], $id);
			}
			elseif (isset($this->request->get['reset'])) {			
			$this->model_tool_collection->resetContent($this->request->post['selected']);
			}
			elseif (isset($this->request->get['import'])) {
				$this->data['ids'] = implode(',', $this->request->post['selected']);
				M('catalog/category');
				$this->data['category_tree'] = $this->model_catalog_category->getCategoryTree();
				$this->display('tool/collection_import.tpl');
				return;
			}
		}
		elseif (isset($this->request->get['importall'])) {
			M('catalog/category');
			$this->data['category_tree'] = $this->model_catalog_category->getCategoryTree();
			$this->display('tool/collection_import.tpl');
			return;
		}
		
		
		$qf = new Query_filter(null, false);	
		$qf->set('nodeid', $id);
		$total = $this->model_tool_collection->getTotalContents($qf);
		$this->data['contents'] = $this->model_tool_collection->getContents($qf);
		
		$page = $qf->get('page', 1);
		$url = "id={$node['id']}";
		if ($qf->get('filter_name')) {
			$url .= "&filter_name=" . urlencode($qf->get('filter_name'));
		}
		if ($qf->get('filter_status') !== null) {
			$url .= "&filter_status=" . $qf->get('filter_status');
		}
		$url = UA('tool/collection/cdata', $url . '&page={page}');
		$this->data['pagination'] = $this->pagination($total, $url, null, $page)->render();
		
		$this->data['filter_name'] = $qf->get('filter_name');
		$this->data['filter_status'] = $qf->get('filter_status');
		
		$this->display('tool/collection_content_list.tpl');
	}
	
	public function ajax_addurl() {
		$json = array('added' => 0);
		if (isset($this->request->post['urls'])) {
			$nodeid = G('id');
			$urls = trim($this->request->post['urls']);
			if ($urls) {
				$urls = explode("\n", str_replace("\r\n", "\n", $urls));
				$data = array();
				foreach ($urls as $url) {
					$data[] = array('url' => $url, 'urlmd5' => md5($url), 'name' => '');
				}
				M('tool/collection');
				$this->model_tool_collection->saveUrls($nodeid, $data);
				$json['added'] = count($data);				
			}
		}
		$this->response->setOutput(json_encode($json));
	}
	
	public function citem() {
		$id = G('id');
		M('tool/collection');
		$content = $this->model_tool_collection->getContent($id);
		$this->data['node'] = $this->model_tool_collection->getNode($content['nodeid']);
		$this->data['url'] = $content['url'];
		$this->data['content'] = $content;
		$this->display('tool/collection_ctest.tpl');
	}
	
	function funcs() {
		$action = G('action');
		
		if ($action == 'save') {
			$func = P('func');
			$content = v5_html_entity_decode(P('content'));
			file_put_contents(DIR_APPLICATION . "controller/tool/cf/$func.php", $content);
			$action = '';
		}
		
		switch ($action) {
			case 'add':				
				$this->data['func'] = 'new_func';
				$this->data['content'] = "<?php\r\n\r\n function ";
				$this->display('tool/collection_func_form.tpl');
				break;
			case 'edit':
				$func = G('func');
				$this->data['func'] = $func;
				$this->data['content'] = file_get_contents(DIR_APPLICATION . "controller/tool/cf/$func.php");
				$this->display('tool/collection_func_form.tpl');				
				break;
			case 'delete':
				if (isset($this->request->post['selected']))
				foreach ($this->request->post['selected'] as $selected) {
					unlink(DIR_APPLICATION . "controller/tool/cf/$selected.php");
				}
			default:
				$files = glob(DIR_APPLICATION . "controller/tool/cf/*.php");
				$funcs = array();
				if ($files) {
					foreach ($files as $file) {
						$funcs[] = str_replace('.php', '', basename($file));
					}
				}
				$this->data['funcs'] = $funcs;
				$this->display('tool/collection_func_list.tpl');
		}
		
	}
	
	public function cimport() {
		set_time_limit(0);
		M('tool/collection');
		$nodeid = G('id');
		$node = $this->model_tool_collection->getNode($nodeid);
		
		$qf = new Query_filter(null, false, $node['col_per_imp']);
		$qf->set('filter_status', 1);
		$ids = $this->request->get('ids');
		
		if ($ids) {
			$ids = explode(',', $ids);
			$_ids = array_slice($ids, 0, $node['col_per_imp']);
			$qf->set('ids', implode(',', $_ids));
			$data = $this->model_tool_collection->getContents($qf, '*');
			$this->_do_import($node, $data);
			if (count($ids) <= $node['col_per_imp']) {
				$this->data['message'] = '导入完成';
			}
			else {
				$ids = implode(',',array_slice($ids, $node['col_per_imp']));
				$this->data['mesage'] = '努力工作中...';
				$this->data['redirect'] = UA('tool/collection/cimport',
						$this->request->query('id,category_id,sku,other1,other2,other3') . '&ids=' . $ids);
				$this->data['wait'] = 2;
			}
		}
		else {
			$qf->set('nodeid', $nodeid);
			$data = $this->model_tool_collection->getContents($qf, '*');
			$this->_do_import($node, $data);
			if (count($data) < $node['col_per_imp']) {
				$this->data['message'] = '导入完成';
			}
			else {
				$this->data['message'] = '努力工作中...';
				$this->data['redirect'] = UA('tool/collection/cimport',
						$this->request->query('id,category_id,sku,other1,other2,other3'));
				$this->data['wait'] = 2;
			}
		}
		
		$this->document->setTitle($this->_['heading_title']);
		$this->data['continue'] = UA('tool/collection/cdata', 'id=' . $node['id']);
		$this->data['heading_title'] = '产品入库';
		
		$this->display('common/message.tpl');
	}
	
	private function _do_import(&$node, $data) {
		$category_id = $this->request->get('category_id');
		$sku = $this->request->get('sku', '');
		$other1 = $this->request->get('other1', '');
		$other2 = $this->request->get('other2', '');
		$other3 = $this->request->get('other3', '');
		
		$product_fields = array('mpn', 'sku', 'model', 'msrp', 'cost');
		$product_description_fields = array('seo_title', 'meta_description', 'meta_keyword');
		
		//product image path
		$image_path = DIR_IMAGE . 'data/' . date('Y/m/');
		$image_dir = 'data/' . date('Y/m/');
		if (!is_dir($image_path)) mkdir($image_path, 0755, TRUE);
	
		$p = $pd = $ids = array();
		
		if ($sku != 'model') {
			$category = $this->db->queryOne("SELECT model_prefix, model_lastid FROM @@category WHERE category_id={$category_id}");
			$model_prefix = $category['model_prefix'];
			$model_lastid = $category['model_lastid'];
		}
		$product_id = $this->db->queryOne("SELECT max(product_id) as product_id FROM @@product");
		
		foreach ($data as $item) {
			$ids[] = $item['id'];
			$model_lastid++;
			$product_id++;
			
			//download images
			$images = $temp = array();
			if ($item['image']) {
				$temp = explode('|', $item['image']);
			}
			foreach ($temp as $i => $image) {
				$filename = "cj-{$item['id']}-{$i}." . get_file_ext($image);
				copy($image, $image_path . $filename);
				if (!@getimagesize($image_path . $filename)) {
					unlink($image_path . $filename);
				}
				else $images[] = $image_dir . $filename;
			}
				
			//product			
			if ($sku != '') {
				if ($sku != 'model') {
					$p['model'] = sprintf("%s%05d", $model_prefix, $model_lastid);
					$p[$sku] = $item['sku'];
				}
				else {
					$p['model'] = $item['sku'];
				}
			}
			$p['category_id'] = $category_id;
			$p['quantity'] = 1000;
			$p['stock_status_id'] = C('config_stock_status_id');
			$p['image'] = !empty($images) ? $images[0] : '';
			$p['price'] = $item['price'];
			$p['weight'] = is_numeric($item['weight']) ? $item['weight'] : 0;
			$p['weight_class_id'] = C('config_weight_class_id');
			$p['length_class_id'] = C('config_length_class_id');
			$p['minimum'] = 1;
			$p['subtract'] = 0;
			$p['status'] = 0;
			$p['shipping'] = 1;
			$p['tax_class_id'] = 0;
			$p['date_available'] = $GLOBALS['V5_NOW'];
			$p['date_added'] = $GLOBALS['V5_NOW'];
			$p['date_modified'] = $GLOBALS['V5_NOW'];
			$p['seo_url'] = trim(preg_replace("/[^a-zA-Z0-9 ]/", " ", $item['name']));
			$p['seo_url'] = preg_replace("/\s+/","-", $p['seo_url']);
			$p['source_link'] = $item['url'];
			if (in_array($other1, $product_fields)) {
				$p[$other1] = $item['other1'];
			}
			if (in_array($other2, $product_fields)) {
				$p[$other2] = $item['other2'];
			}
			if (in_array($other3, $product_fields)) {
				$p[$other3] = $item['other3'];
			}
			if (C('config_seo_url')) {
				$p['link'] = U('product/product', "category_id={$category_id}&product_id={$product_id}&seo_url={$p['seo_url']}");
			}
			else {
				$p['link'] = U('product/product', "product_id={$product_id}");
			}
			$p['link'] = str_replace(HTTP_CATALOG, '', $p['link']);
			
			$this->db->insert('product', $p);
				
			//product description
			$pd['product_id'] = $product_id;
			$pd['language_id'] = 1;
			$pd['name'] = $item['name'];
			$pd['description'] = $this->_clear_link($this->_save_remote_picture($item['description']));
			if (in_array($other1, $product_description_fields)) {
				$pd[$other1] = $item['other1'];
			}
			if (in_array($other2, $product_description_fields)) {
				$pd[$other2] = $item['other2'];
			}
			if (in_array($other3, $product_description_fields)) {
				$pd[$other3] = $item['other3'];
			}
			
			$this->db->insert('product_description', $pd);
				
			//product images
			if(!empty($images)) {
				$sql = "INSERT INTO @@product_image(product_id, image) VALUES({$product_id}, '{$images[0]}')";
				for ($i = 1, $c =  count($images); $i < $c; $i++) {
					$sql .= ", ({$product_id}, '{$images[$i]}')";
				}
				$this->db->runSql($sql);
			}
				
			//product category
			$this->db->runSql("INSERT INTO @@product_to_category(product_id,category_id) VALUES($product_id, $category_id)");
				
			//product store
			$this->db->runSql("INSERT INTO @@product_to_store(product_id,store_id) VALUES($product_id, 0)");
		}
		if ($sku != 'model') {
			$this->db->runSql("UPDATE @@category SET model_lastid = $model_lastid WHERE category_id = $category_id");
		}
		$ids = implode(',', $ids);
		$time = $GLOBALS['V5_TIME'];
		$this->db->runSql("UPDATE @@collection_content SET status = 2,imptime=$time WHERE id IN ($ids)");
	}
	
	private function _save_remote_picture($content, $ext = 'jpg|jpeg|gif|png|bmp') {
		if(!preg_match_all("/src=([\"|']?)([^ \"'>]+\.($ext))\\1/i", $content, $matches)) return $content;
		
		$urls = $oldpath = $newpath = array();
		$base_url = HTTP_CATALOG;
	
		$filedir = $image_path = DIR_IMAGE . 'data/upload/' . date('Y/m/');
		$filepath = $base_url . 'image/data/upload/' . date('Y/m/');
		is_dir($filedir) or mkdir($filedir, 0755, TRUE);
	
		foreach($matches[2] as $k => $url) {
			if(in_array($url, $urls)) continue;
			$urls[$url] = $url;
			if(strpos($url, '://') === false) continue;
			if(strpos($url, $base_url) !== false) continue;
			$fileext = get_file_ext($url);
			$filename = uniqid() . '.' . $fileext;
			$newfile = $filedir . $filename;
			$fixedurl = $this->_fix_url($url);
			$pos = strrpos($fixedurl, '/');
			//copy function will not work if the filename contain chinese characters, so use <b>unrlencode</b> function here
			$fixedurl = substr($fixedurl, 0, $pos) . '/' . urlencode(substr($fixedurl, $pos + 1));
			if(copy($fixedurl, $newfile)) {
				if(preg_match("/^(jpg|jpeg|gif|png|bmp)$/i", $fileext)) {
					if(!@getimagesize($newfile)) {
						unlink($newfile);
						continue;
					}
				}
				$oldpath[] = $url;
				$newurl = $filepath . $filename;
				$newpath[] = $newurl;
			}
		}
		unset($matches);
		return str_replace($oldpath, $newpath, $content);
	}
	
	private function _clear_link($html) {
		$html = preg_replace("/<a[^>]*>/i", "", $html);
		return preg_replace("/<\/a>/i", "", $html);
	}
	
	private function _fix_url($url) {
		$parts = parse_url($url);
		$path = str_replace('/./', '/', $parts['path']);
		while(1) {
			$temp = $path;
			$path = preg_replace('/^\/\.\.\//', '/', $path);
			if($path == $temp) break;
		}
	
		return $parts['scheme'] . '://' . $parts['host'] . $path;
	}
}